﻿using System;
using Microsoft.Practices.Unity;
using NUnit.Framework;
using Rhino.Mocks;

namespace FireStarterModeller.Test
{
    public interface IFoo
    {
        string Bar { get; set; }
        string GetBar();
    }    
    public class Foo: IFoo
    {
        public string Bar
        {
            get; set;
        }

        public string GetBar()
        {
            return "bar";
        }
    }

    [TestFixture]
    public class TestAutoMockingUnityContainer
    {
        [Test]
        public void Test_ResolveWithNormalContainer_ShouldRaiseError()
        {
            //---------------Set up test pack-------------------
            IUnityContainer container = new UnityContainer();
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            try
            {
                container.Resolve<IFoo>();
                Assert.Fail("Expected to throw an ");
            }
                //---------------Test Result -----------------------
            catch (ResolutionFailedException ex)
            {
                StringAssert.Contains("Resolution of the dependency failed", ex.Message);
            }
        }

        [Test]
        public void Test_ResolveWithAutoMockingContainer_ShouldReturnMock()
        {
            //---------------Set up test pack-------------------
            IUnityContainer container = new AutoMockingUnityContainer();
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            var resolvedObject = container.Resolve<IFoo>();
            //---------------Test Result -----------------------
            Assert.IsInstanceOf(typeof (IFoo), resolvedObject);

        }
        [Test]
        public void Test_SetPropValue_WhenAutoMocking_ShouldSetPropValue()
        {
            //---------------Set up test pack-------------------
            IUnityContainer container = new AutoMockingUnityContainer();
            const string expected = "ABar";
            var resolvedObject = container.Resolve<IFoo>();
            //---------------Assert Precondition----------------
            Assert.IsInstanceOf(typeof(IFoo), resolvedObject);
            //---------------Execute Test ----------------------
            resolvedObject.Bar = expected;
            //---------------Test Result -----------------------
            Assert.AreEqual(expected, resolvedObject.Bar);
        }
        [Test]
        public void Test_SetReturnValue_WhenAutoMocking_ShouldReturnValue()
        {
            //---------------Set up test pack-------------------
            IUnityContainer container = new AutoMockingUnityContainer();
            const string expected = "ABar";
            var resolvedObject = container.Resolve<IFoo>();
            //---------------Assert Precondition----------------
            Assert.IsInstanceOf(typeof(IFoo), resolvedObject);
            //---------------Execute Test ----------------------
            resolvedObject.Stub(foo => foo.GetBar()).Return(expected);
            //---------------Test Result -----------------------
            Assert.AreEqual(expected, resolvedObject.GetBar());
        }
    }
}
